package com.fjrcloud.service;

import com.fjrcloud.dao.HousingStructureDao;
import com.fjrcloud.dao.HousingStructureIntoNewDao;
import com.fjrcloud.dao.HousingStructureLevelDao;
import com.fjrcloud.entity.HousingStructure;
import com.fjrcloud.entity.HousingStructureIntoNew;
import com.fjrcloud.entity.HousingStructureLevel;
import com.fjrcloud.exception.BusinessException;
import com.fjrcloud.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

/**
 * Created by shengjian.lin on 17-1-18.
 */
@Service
public class HousingStructureService {

    @Autowired
    private HousingStructureDao structureDao;
    @Autowired
    private HousingStructureLevelDao structureLevelDao;
    @Autowired
    private HousingStructureIntoNewDao structureIntoNewDao;

    public HousingStructure saveStructure(HousingStructure structure){

        structure.setCreateTime(System.currentTimeMillis());

        return structureDao.save(structure);
    }
    public HousingStructure getStructure(Integer id){
        return structureDao.findOne(id);
    }
    public void delStructure(Integer id){
        structureDao.delete(id);
    }

    public Page<HousingStructure> listStructure(Integer pageNum, Integer pageSize){

        pageNum = PageUtils.checkPageNum(pageNum);
        pageSize = PageUtils.checkPageSize(pageSize);
        PageRequest pageRequest = new PageRequest(pageNum, pageSize);

        return structureDao.findByValidOrderByCreateTimeAsc(true,pageRequest);
    }

    public HousingStructureLevel saveStructureLevel(Integer structureId, HousingStructureLevel level) throws BusinessException {

        if (structureId == null)
            throw new BusinessException("房屋结构主体缺损");

        HousingStructure structure = structureDao.findOne(structureId);
        if (structure == null)
            throw new BusinessException("房屋结构主体缺损");

        level.setStructure(structure);

        level.setCreateTime(System.currentTimeMillis());

        return structureLevelDao.save(level);
    }
    public HousingStructureLevel getStructureLevel(Integer id){
        return structureLevelDao.findOne(id);
    }
    public void delStructureLevel(Integer id){
        structureLevelDao.delete(id);
    }

    public Page<HousingStructureLevel> listStructureLevel(Integer structureId, Integer pageNum, Integer pageSize) throws BusinessException {

        pageNum = PageUtils.checkPageNum(pageNum);
        pageSize = PageUtils.checkPageSize(pageSize);
        PageRequest pageRequest = new PageRequest(pageNum, pageSize);

        if (structureId == null)
            throw new BusinessException("查询房屋结构主体缺损");

        HousingStructure structure = structureDao.findOne(structureId);
        if (structure == null)
            throw new BusinessException("查询房屋结构主体错误");

        return structureLevelDao.findByStructureAndValidOrderByCreateTimeAsc(structure,true,pageRequest);
    }
    public Page<HousingStructureLevel> listStructureLevelAll(Integer pageNum, Integer pageSize) {

        pageNum = PageUtils.checkPageNum(pageNum);
        pageSize = PageUtils.checkPageSize(pageSize);
        PageRequest pageRequest = new PageRequest(pageNum, pageSize);

        return structureLevelDao.findByValidOrderByCreateTimeAsc(true,pageRequest);
    }

    public HousingStructureIntoNew saveStructureIntoNew(HousingStructureIntoNew intoNew){

        intoNew.setCreateTime(System.currentTimeMillis());

        return structureIntoNewDao.save(intoNew);
    }
    public HousingStructureIntoNew getStructureIntoNew(Integer id){
        return structureIntoNewDao.findOne(id);
    }
    public void delStructureIntoNew(Integer id){
        structureIntoNewDao.delete(id);
    }

    public Page<HousingStructureIntoNew> listStructureIntoNew(Integer pageNum, Integer pageSize){

        pageNum = PageUtils.checkPageNum(pageNum);
        pageSize = PageUtils.checkPageSize(pageSize);
        PageRequest pageRequest = new PageRequest(pageNum, pageSize);

        return structureIntoNewDao.findByValidOrderByCreateTimeAsc(true,pageRequest);
    }
}
